-- unload_grid
-- create by panyl
-- 技能: 拆卸格子（使失效）


return {
    apply = function(source, target, skillId, round, para, extra)
        -- 1. 格子类型和数量
        local class = para[1];
        local count = para[2];

        -- 先找出所有该类格子
        local arr = {};
        for pos = 1, GRID_SIZE do
            local grid = DungeonM.getGridByPos(pos);
            if grid.class == class and grid:isOpened() and grid.state ~= GRID_STATE_DISABLE then
                table.insert(arr, grid);
            end
        end

        -- 没有目标
        local total = #arr;
        if total <= 0 then
            return {};
        end

        -- 随机几个
        count = math.min(count, total);
        local ret = {};
        for i = 1, count do
            local index = DungeonM.getRandSeed("unload_grid") % total + 1;
            local grid = arr[index];

            -- 状态置为失效
            grid:changeState(GRID_STATE_DISABLE);

            arr[index] = arr[total];
            total = total - 1;

            table.insert(ret, grid);
        end

        -- 抛出事件
        EventMgr.fire(event.UNLOAD_GRID, { ["grids"] = ret, });

        -- 作用目标
        return ret;
    end,

    parse = function(desc)
        local arr = string.explode(desc, ",");
        return { tonumber(arr[1]), tonumber(arr[2]), };
    end,

    -- 解析技能描述
    parseDesc = function(source, skillId, desc, para)
        return desc;
    end,
}

